<template>
	<view class="content">
		<swiper style="box-sizing: border-box;background-size: 100% 100%;" class="wid100 dis disAl tu1" circular :autoplay='true' indicator-active-color="#3E86FF">
			<swiper-item v-for="(item,index) in banner" :key="index">
				<image :src="ossurl+item.image" class="wh100" mode="aspectFill"></image>
			</swiper-item>
		</swiper>
		<view class="wid87 mar dis disAl disJuB mar-top40">
			<view :style="'box-sizing: border-box;background-size: 100% 100%;background-image: url('+imageUrl+'/tu2.png);'"
				class="dis disAl disJuC tu2 foSi40 fowe600" @click="tiaozhuan('/pages/wishing/drawLots')">每日一拜</view>
			<view :style="'box-sizing: border-box;background-size: 100% 100%;background-image: url('+imageUrl+'/tu2.png);'"
				class="dis disAl disJuC tu2 foSi40 fowe600" @click="tiaozhuan('/pages/wishing/wishing')">祈福许愿</view>
			<view :style="'box-sizing: border-box;background-size: 100% 100%;background-image: url('+imageUrl+'/tu2.png);'"
				class="dis disAl disJuC tu2 foSi40 fowe600" @click="tiaozhuan('/pages/wishing/animals')">生日放生</view>
		</view>
		<view class="mar-top40 wid87 mar foSi50 fowe600">操作资讯</view>
		<view class="mar-top30 wid87 mar">
			<block v-for="(item,index) in list" :key="index">
				<view class="wid100 dis disAl" style="background-color: #F7F2EE;" @click="tiaozhuan('/pages/common/richtext?id='+item.id)">
					<view style="width: 211rpx;height: 191rpx;">
						<image :src="ossurl+item.image" class="wh100"></image>
					</view>
					<view class="mar-left20" style="color: #5B5B5B;width: 60%;">
						<view class="foSi30" style="color: #000;">{{item.title}}</view>
						<view class="foSi20 mar-top20 wid100 webkitLineClamp">{{item.des}}</view>
						<view class="foSi18 mar-top20">{{item.createtime | formatDate}}</view>
					</view>
				</view>
				<view style="height: 20rpx;"></view>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				banner:[],
				url:this.global.url,
				ossurl:this.global.ossurl,
				imageUrl: this.global.imageUrl,
			}
		},
		onLoad() {
			var that = this;
				that.alioss();
			var userinfo = uni.getStorageSync('userinfo')
			// #ifdef H5
			var option = that.getUrlCode()
			if(!userinfo){
				if(option.code){
					that.getOpenID(option.code)
				}else{
					that.getWxCode()
				}
			}else{
				setTimeout(() => {
					that.indexList()
				},1000)
			}
			// #endif
			// #ifdef MP-WEIXIN
				setTimeout(() => {
					that.indexList()
				},1000)
			// #endif
		},
		methods: {
			alioss(){
				var that  = this;
				uni.request({
				    // http://jufeng.tengshuokeji.cn//yonghu/order/order_comments
				    url: that.global.url + '/api/index/alioss',
				    method: "POST",
				    data: {},
				    success(res) {
				        let result = res.data
				        console.log(result)
				        if (result.code == 1) {
				            that.global.ossurl = result.data
							console.log(that.global.ossurl);
							that.ossurl = that.global.ossurl;
				        }
				    }
				})
			},
			getWxCode(){
				var that = this
				uni.request({
					url: that.global.url+'/api/User/getCode',
					method: 'POST',
					header:{},
					data: {},
					success(res) {
					    let result = res.data
					    if(result.code == 1){
							// 有style的写法
							// console.log(result.data);
							let appid = result.data; //为测试号id
							let local = window.location.href;
							// 页面A1 首次授权
							window.location.href =
								`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
						}else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none',
								duration: 2000
					        })
					    }
					}
				})
			},
			getUrlCode() {
				// 截取url中的code方法
				var url = location.search;
				var theRequest = new Object();
				if (url.indexOf("?") != -1) {
					var str = url.substr(1);
					var strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
					}
				}
				return theRequest;
			},
			getOpenID(code){
				var that = this
				uni.request({
					url: that.global.url+'/api/User/getOpenId',
					method: 'POST',
					header:{},
					data: {
						code: code
					},
					success(res) {
					    let result = res.data
					    if(result.code == 1){
							console.log(result.data);
							uni.setStorageSync('userinfo', result.data.userinfo)
							uni.setStorageSync('token',result.data.token)
				            uni.setStorageSync('openid', result.data.openid)
							if(result.data.mobile){
								uni.setStorageSync('mobile', result.data.mobile)
							}
							
							window.location.href = location.href.split('?')[0]
							// that.indexList()
						}else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none',
								duration: 2000
					        })
					    }
					}
				})
			},
			indexList(){
				var that = this
				uni.request({
					url: that.global.url+'/api/index', 
					data: {},
					header: {
						 'Content-Type': 'application/json' //自定义请求头信息
					},
					method:'POST',//请求方式，必须为大写
					success: (res) => {
						that.banner = res.data.data.banner
						that.list = res.data.data.news
					}
				})
			},
			tiaozhuan(url){
				uni.navigateTo({
					url: url
				})
			}
		},
		filters: {
			formatDate(value) {
				let date = new Date(value * 1000);
				//时间戳为10位需*1000，时间戳为13位的话不需乘1000
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				let h = date.getHours();
				h = h < 10 ? ('0' + h) : h; //小时补0
				let m = date.getMinutes();
				m = m < 10 ? ('0' + m) : m; //分钟补0
				let s = date.getSeconds();
				s = s < 10 ? ('0' + s) : s; //秒补0
				return y + '-' + MM + '-' + d; //年月日
				// return y + '-' + MM + '-' + d + ' ' + h + ':' + m+ ':' + s; //年月日时分秒

			}
		}
	}
</script>

<style>
	.content {
		min-height: 100vh;
		background-color: #F4EDDD;
	}
	.tu1{
		height: 456rpx;
	}
	.tu2{
		width: 30%;
		height: 128rpx;
		color: #FEEDBB;
		line-height: 41rpx;
	}
</style>
